//
//  GasStationDriverManager.m
//  NtoccDriverNew
//
//  Created by niyongsheng on 2022/10/18.
//  Copyright © 2022 GCY. All rights reserved.
//

#import "GasStationDriverManager.h"

#define GasStationDriverManagerCacheKey  @"GasStationDriverManagerCacheKey"

@implementation GasStationDriverModel
+ (NSDictionary *)modelCustomPropertyMapper {
    return @{
        @"desc" : @"description",
        @"ID" : @"id",
        @"xid" : @[@"id"]
    };
}
@end

@implementation GasStationDriverManager
+ (GasStationDriverManager *)sharedGasStationDriverManager {
    static GasStationDriverManager *sharedUserManager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedUserManager = [[self alloc] init];
    });
    return sharedUserManager;
}

- (void)refreshUserInfo {
    [self refreshUserInfo:nil];
}

- (void)refreshUserInfo:(GasStationDriverManagerCompletion)completion {
    NSString *urlStr = CJStringWithFormat(@"%@%@",[NSString stringWithFormat:@"%@/ntocc-basic-api",[ReAddressDynamic shared].baseUrl],GasDriverExt);
    NSDictionary *branch = [UserDefaultUtil valueForKey:Login_Branch];
    NSDictionary *params = @{@"driverCode": branch[@"code"]};
    @weakify(self)
    [HTTPManager getNetworkRequest:urlStr
                     withParameter:params
                            withVC:[UIViewController new]
                           netName:@"加油站司机信息"
                           success:^(id response, NSString *msg) {
        @strongify(self)
        GasStationDriverModel *userInfo = [GasStationDriverModel mj_objectWithKeyValues:response];
        self.userInfo = userInfo;
        
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        [userDefaults setValue:response forKey:GasStationDriverManagerCacheKey];
        [userDefaults synchronize];
        if (completion) {
            completion(userInfo);
        }
    } failure:^(NSInteger code, NSString *msg) {
        
    }];
}

- (GasStationDriverModel *)userInfo {
    if (!_userInfo) {
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        NSDictionary *cacheDict = [userDefaults dictionaryForKey:GasStationDriverManagerCacheKey];
        _userInfo = [GasStationDriverModel mj_objectWithKeyValues:cacheDict];
    }
    return _userInfo;
}

- (void)clearUserInfo {
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults removeObjectForKey:GasStationDriverManagerCacheKey];
}

- (BOOL)isNetNormal {
    return [AFNetworkReachabilityManager sharedManager].reachable;
}
@end
